import ass.*;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import ass.common.DBConnectionFactory;
import ass.common.ServiceLocatorException;
import ass.dao.support.*;

/**
 * Servlet implementation class AddUserServlet
 */
public class AddUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public AddUserServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		
		try {
	
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			
			DBConnectionFactory services = new DBConnectionFactory();
			Connection conn = services.createConnection(); 
			
			//insert a record example
			PreparedStatement stmt = conn.prepareStatement(
		       "insert into TBL_MOVIES values (4, 'Royy', 'abcde')");
		    
		    int n = stmt.executeUpdate();
		    
		     if (n!= 1){
		    	 System.out.print("insert failed");
		     }
		    
		     //query example
		     String sql = "select * from tbl_movies";
		     PreparedStatement pstmt = conn.prepareStatement(sql);
				ResultSet rs = pstmt.executeQuery();
		     
		    
			
			while (rs.next()) {
				out.println("name: " + rs.getString("title") + "</br>");
			}
			
			/*
			need to implement findAllByUser first
			MovieDAOImpl movieDAO = new MovieDAOImpl();
			List movieList = movieDAO.findAllByUser(1);
			
			request.setAttribute("movieList", movieList); //fist time login
			RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
			rd.forward(request, response);
			*/

			rs.close();
			pstmt.close();
			conn.close();

		}  catch (SQLException e) {
			e.printStackTrace();
		} catch (ServiceLocatorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
